import datetime
import random
from conn_mysql import ApiSql
from .phone_test import is_phone
class SendSms(ApiSql):

    def __init__(self,phone):

        self.phone=phone
        self.verify_code=random.randint(100000,999999)
        super().__init__()
  
    def verification_code(self):

        if not self.phone and not is_phone(self.phone):

            return {'status':-1,'errorCode':1,'errorMsg':'请检查你的手机号','resultBody':None}

        info=self.insert_verify_code(self.verify_code,self.phone)
     
        return {'status':1,'errorCode':None,'errorMsg':None,'resultBody':info}

    def can_i_sms(self):
        '''phone是否可以发短信'''

        sql = 'SELECT * FROM phone_verify WHERE phone=%s ORDER BY id DESC LIMIT 0,1'
        result=self.select_one(sql,(self.phone,))
        if result:

            date_delta=datetime.timedelta(minutes=5)
            register=result['register_time']
            now=datetime.datetime.now()

            if (date_delta+register)>now:

                return False

        return True


    def send_sms(self):

        return {

            'status':1,
            'errorCode':None,
            'errorMsg':None,
            'resultBody':'短信已发送'
        }







        




        







